//
//  WYCHuankuanViewController.m
//  掌上还
//
//  Created by 王晓民 on 2018/3/2.
//  Copyright © 2018年 中企互联. All rights reserved.
//

#import "WYCHuankuanViewController.h"
#import "WYCBillModel.h"
#import "WYCBillFrameModel.h"
#import "WYCHomeCell.h"
#import "WYCSeeBillController.h"
#import "WYCAddCreditController.h"
#import "WYCHandBillController.h"

@interface WYCHuankuanViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView  *tableView;
@property(nonatomic,strong)NSMutableArray * billarray;
/** 背景*/
@property(nonatomic,strong)UIButton * zView;


/** view*/
@property(nonatomic,strong)UIView * ejectView;
@end

@implementation WYCHuankuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WYCGlobalBg;
    self.billarray = [[NSMutableArray alloc] init];

    [self setupTableView];
    [self setupCreditcardlist];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupCreditcardlist)];

}
#pragma make ------->>>列表请求
- (void)setupCreditcardlist{
    
    [self.billarray removeAllObjects];
    NSString *cardlist = [NSString stringWithFormat:@"%@token=%@",creditcardlist,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:cardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                NSLog(@"智能还款%@",responseObject);
                WYCBillModel * model = [WYCBillModel mj_objectWithKeyValues:dict];
                if ([model.isHideCard isEqualToString:@"1"]) {
                    continue;
                }else{
                    WYCBillFrameModel * BillFrameModel = [[WYCBillFrameModel alloc] init];
                    BillFrameModel.Billmodel = model;
                    
                    [self.billarray addObject:BillFrameModel];
                    
                    if (self.billarray.count == 0) {
                        self.tableView.hidden = YES;
                      // [self ObtainButton];
                    }else{
                        self.tableView.hidden = NO;
                    //   [self.billView setHidden:YES];
                    }
                }
            }
            
            
            
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    }];
    
}
#pragma mark ------>>创建tableview
- (void)setupTableView{
    
    UIView  *footview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    footview.backgroundColor = [UIColor clearColor];
    footview.userInteractionEnabled = YES;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.estimatedSectionHeaderHeight = 0;
    
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50 * px + (NavHeight), ScreenWidth, ScreenHeight - 50 * px - (NavHeight)) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = footview;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    UIButton * cellbutton = [[UIButton alloc] init];
    cellbutton.frame = CGRectMake(10, 0, ScreenWidth-20, 40);
    cellbutton.titleLabel.font = [UIFont systemFontOfSize:WyzFont];
    [cellbutton setTitleColor:wycMain forState:UIControlStateNormal];
    cellbutton.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
    [cellbutton addTarget:self action:@selector(addbankButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [cellbutton setTitle:@"+添加信用卡" forState:UIControlStateNormal];
    cellbutton.backgroundColor = [UIColor whiteColor];
    [footview addSubview:cellbutton];
}
-(void)addbankButtonClick
{
    NSLog(@"添加信用卡");
    WYCAddCreditController  *addBank = [[WYCAddCreditController alloc]init];
    [self.navigationController pushViewController:addBank animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.billarray.count>0) {
        return self.billarray.count;
    }
    else{
        return 1;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * strID = @"cell";
    
    WYCHomeCell * cell = [tableView dequeueReusableCellWithIdentifier:strID];
    
    if (cell == nil)
    {
        
        cell=[[[NSBundle mainBundle]loadNibNamed:@"WYCHomeCell" owner:self options:nil]lastObject];
        
    }
    if (self.billarray.count>0) {
        cell.billmodelFrame = self.billarray[indexPath.row];
        
    }else
    {
        cell.userInteractionEnabled = NO;
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = [UIColor clearColor];
    if ([cell.repaymentButton.titleLabel.text isEqualToString:@"立即认证"]) {
        [cell.repaymentButton addTarget:self action:@selector(repayMentClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYCBillFrameModel * frameModel = self.billarray[indexPath.row];
    
    if ([frameModel.Billmodel.approve isEqualToString:@"2"]) {
        
        return;
    }else{
        
        WYCSeeBillController * SeeBill = [[WYCSeeBillController alloc] init];
        //银行卡账号
        SeeBill.cardId = frameModel.Billmodel.cardId;
        
        //银行卡姓名
        SeeBill.bankName = frameModel.Billmodel.bankName;
        
        //银行卡后四位
        SeeBill.lastNumber = frameModel.Billmodel.lastNumber;
        
        //银行卡多少天出账单
        SeeBill.outbBllDay = frameModel.Billmodel.outbBllDay;
        
        //本期账单金额
        SeeBill.billAmount = frameModel.Billmodel.billAmount;
        
        
        //免息期
        SeeBill.gracePeriod = frameModel.Billmodel.gracePeriod;
        
        
        //总额度
        SeeBill.creditLine = frameModel.Billmodel.creditLine;
        
        
        //还款日
        SeeBill.dueDate = frameModel.Billmodel.dueDate;
        
        
        //账单日
        SeeBill.billDate = frameModel.Billmodel.billDate;
        
        
        SeeBill.userCard = frameModel.Billmodel.userCard;
        SeeBill.bankType = frameModel.Billmodel.state;
        
        //
        [self.navigationController pushViewController:SeeBill animated:NO];
    }
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    
    CALayer* bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCRGBColor(191, 191, 196).CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(NavView.width - 96 * px, titleY, 70 * px , 70 * px);
    [NavView addSubview:backbutton];
    
    
    
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"我的账单";
    [NavView addSubview:NavRigsterLabel];
}
-(void)repayMentClick
{
    WYCAddCreditController  *addBank = [[WYCAddCreditController alloc]init];
    [self.navigationController pushViewController:addBank animated:YES];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma make ------>>右侧按钮 点击事件
- (void)addClick{
    
    WYCLogFunc;
    
    
    UIView * ejectView = [[UIView alloc] init];
    ejectView.frame = CGRectMake(0, ScreenHeight - 500 * px, ScreenWidth, 500 * px);
    ejectView.backgroundColor = [UIColor whiteColor];
    [[UIApplication sharedApplication].keyWindow  addSubview:ejectView];
    self.ejectView = ejectView;
    
    
    UIButton * zView = [[UIButton alloc] init];
    zView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - ejectView.height);
    zView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [zView addTarget:self action:@selector(zviewClick) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow  addSubview:zView];
    self.zView = zView;
    
    
    UIButton * holdButton = [[UIButton alloc] init];
    holdButton.frame = CGRectMake(0,  25 * px, ejectView.width, 100 * px);
    [holdButton setTitle:@"手输账单" forState:UIControlStateNormal];
    holdButton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [holdButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    holdButton.contentHorizontalAlignment =
    UIControlContentHorizontalAlignmentCenter;
    [holdButton addTarget:self action:@selector(holdButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [ejectView addSubview:holdButton];
    
    
    UIView * line = [[UIView alloc] init];
    line.backgroundColor = WYCGlobalBg;
    line.frame = CGRectMake(0, CGRectGetMaxY(holdButton.frame) + 25 * px, ejectView.width, 20 * px);
    [ejectView addSubview:line];
    
    
    UIButton * NoButton = [[UIButton alloc] init];
    NoButton.frame = CGRectMake(0, CGRectGetMaxY(line.frame) + 25 * px, ejectView.width, 100 * px);
    [NoButton setTitle:@"取消" forState:UIControlStateNormal];
    NoButton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [NoButton addTarget:self action:@selector(NoButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [NoButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    NoButton.contentHorizontalAlignment =
    UIControlContentHorizontalAlignmentCenter;
    [ejectView addSubview:NoButton];
    
}

- (void)holdButtonClick{
    WYCLogFunc;
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    WYCHandBillController * handbill = [[WYCHandBillController alloc] init];
    [self.navigationController pushViewController:handbill animated:NO];
}

- (void)NoButtonClick{
    WYCLogFunc;
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    
}
- (void)zviewClick{
    WYCLogFunc;
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    
}

- (void)importClick{
    [self addClick];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
